package applet;

import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.Border;

import P2PConnector.P2PConnector;

public class chat_test2 extends Applet implements ActionListener{
	
	String user_name = "Kim";
	
	String user1 = "Johnson";
	
	private static final long serialVersionUID = -9139034459056627015L;
	// class variables
	JTextArea bulletin_board;
	JTextArea user_list;
	JTextArea my_message;
	
	JButton send_message;
	JButton log_out;
	JButton get_users;
	
	P2PConnector p2p;
	
	server_connector_test server;

	/** applet lifecycle is started by init()**/
	public void init() {
		bulletin_board = new JTextArea(400, 100);
		bulletin_board.setAutoscrolls(true);
		
		user_list = new JTextArea(200, 100);
		user_list.setAutoscrolls(true);
		
		my_message = new JTextArea(1000, 10);
		
		this.setLayout(new GridLayout(0,2));
		this.add(bulletin_board,-1);
		Border b = BorderFactory.createLineBorder(this.getForeground());
		bulletin_board.setBorder(b);
		
		
		this.add(user_list,-1);
		user_list.setBorder(b);
		
		get_users = new JButton("Get User List...");
		JPanel get_user_frame = new JPanel();
		get_user_frame.setLayout(new BorderLayout());
		get_user_frame.add(get_users, BorderLayout.SOUTH);
		log_out = new JButton("Log Out...");
		get_user_frame.add(log_out, BorderLayout.NORTH);
		send_message = new JButton("Send Message...");
		get_user_frame.add(send_message, BorderLayout.CENTER);
		this.add(get_user_frame);
		
		
		
		this.add(my_message,-1);
		my_message.setBorder(b);

		this.setSize(750, 500);
		this.setVisible(true);
		this.repaint();
		this.addMessage("System", "there are no messages");
	
		send_message.addActionListener(this);
		get_users.addActionListener(this);
		log_out.addActionListener(this);
		
		p2p = new P2PConnector(user_name, bulletin_board);
		
		server = new server_connector_test();
	}

	/**adds a message to this Client's board ie: for later display**/
	public void addMessage(String user, String message) {
		bulletin_board.append(user + ">> " + message);
		bulletin_board.repaint();
	}


	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		String s = arg0.getActionCommand();
		
		if(s.equals("Send Message...")) {
			String send_to_user_id = user_list.getSelectedText();
			String message = my_message.getText();
			System.out.println(message);
			//short circuiting this to send to user1
			if(p2p.sendMessage(this.user1, message)) {
				System.out.println("\nPrinted\n");
				this.addMessage(this.user_name, message + "\n");
			}
			
		} else if (s.equals("Get User List...")) {
			HashMap<String, InetSocketAddress> u_list = server.getUsers(user_name);
			Iterator<Entry<String, InetSocketAddress>> i = u_list.entrySet().iterator();
			Entry<String, InetSocketAddress> val;
			String str;
			user_list.setText("");
			while (i.hasNext()) {
				val = i.next();
				String id = val.getKey();
				InetSocketAddress new_addr = val.getValue();
				if (!val.getKey().equals(user_name)) {
					str = val.getKey() + " " + val.getValue() + "\n";
					user_list.append(str);
					p2p.addConnection(id,new_addr);
				}
			}
			user_list.repaint();
			
			
			
		} else if(s.equals("Log Out...")) {
			server.disconnect(user_name);
			System.out.println("\nLog Out on: " + user_name + "\n");
			//clear user list, etc
		}
	}
}
